package businesslogic.StudentBl;

import java.util.ArrayList;

import po.CoursePO;
import po.StudentPO;
import vo.StudentVO;
import appendix.ResultMessage;
import businesslogic.ChooseCourseBl.ChooseCourse;
import businesslogic.CourseBl.Course;
import businesslogic.LoginBl.LoginController;
import businesslogic.MessageBl.Message;
import businesslogic.PlanBl.Plan;
import businesslogicservice.StudentBlService;

public class StudentController implements StudentBlService {
	private StudentPO po;
	
	public StudentController(){
		po=(StudentPO)LoginController.getUserPO();
	}

	@Override
	public ResultMessage<?> showCourse() {
		ChooseCourse record=new ChooseCourse();
		ResultMessage<?> rm=record.searchCourseList(po.getID());
		return rm;
	}

	@Override
	public ResultMessage<?> chooseCourse(long lesson_id) {
		if(po.getOpNum()==4){
			return new ResultMessage<Boolean>(false);
		}else{
			po.setOpNum(po.getOpNum()+1);
			Student student=new Student();
			student.updateStudent(po);
			ChooseCourse record=new ChooseCourse();
			int chance=po.getOpCredit()+(int)(Math.random()*14)+1;;
			ResultMessage<?> rm=record.chooseCourse(chance, po.getID(), lesson_id);
			return rm;
		}
	}

	@Override
	public ResultMessage<?> quitCourse(long lesson_id) {
		ChooseCourse record=new ChooseCourse();
		ResultMessage<?> rm=record.quitCourse(po.getID(), lesson_id);
		if((Boolean)rm.result){
			po.setOpNum(po.getOpNum()-1);
			Student student=new Student();
			student.updateStudent(po);
		}
		return rm;
	}

	@Override
	public ResultMessage<?> showScore(long lesson_id) {
		ChooseCourse record=new ChooseCourse();
		ResultMessage<?> rm=record.showScore(po.getID(), lesson_id);
		return rm;
	}

	@Override
	public ResultMessage<?> showSelfInfo() {
		StudentVO vo=new StudentVO(po);
		return new ResultMessage<StudentVO>(vo);
	}

	@Override
	public ResultMessage<?> showOutline(){
		Plan plan=new Plan();
		return plan.getOutline();
	}
	
	

	@Override
	public ResultMessage<?> changePassword(String oldOne, String newOne1,
			String newOne2) {
		boolean result=po.isPassword(oldOne);
		if(result){
			if(newOne1.equals(newOne2)){
				po.setPassword(newOne1);
				Student student=new Student();
				student.updateStudent(po);
			}else{
				result=false;
			}
		}
		return new ResultMessage<Boolean>(result);
	}

	@Override
	public ResultMessage<?> showPlan() {
		Plan plan=new Plan();
		return plan.getPlan(po.getDepartment());
	}

	@Override
	public ResultMessage<?> getMessageList() {
		Message message =new Message();
		return message.getMessage(po.getID());
	}

	@Override
	public ResultMessage<?> getOpCourse() {
		Course course=new Course();
		return new ResultMessage<ArrayList<CoursePO>>(course.getOpCourse());
	}

	@Override
	public ResultMessage<?> getChooseOpCourse() {
		ChooseCourse record=new ChooseCourse();
		return record.getChooseOpCourse(po.getID());
	}

	public ResultMessage<?> conflictExam() {
		ChooseCourse record=new ChooseCourse();
		return record.conflictExamForStudent(po.getID());
	}
}
